﻿using CraftParameterConfig.Domain.Entity;
using CraftParameterConfig.Domain.Interfaces;
using CraftParameterConfig.Infrastructure.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CraftParameterConfig.Infrastructure.Data
{
    public class TableDataRespository : RespositoryBase, ITableDataRespository
    {
        DbContext dbContext = GlobalObjects.DbContext;
        ObjectChange obc = GlobalObjects.ObjectChange;
        public TableDataRespository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }
        public void Add(TableDataConfig entity)
        {
            string sql = GetInsertSql(entity);
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void AddRange(List<TableDataConfig> entitys)
        { 
            List<string> sql = new List<string>();
            foreach (var item in entitys)
            {
                sql.Add(GetInsertSql(item));
            }
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void Delete(Guid id)
        {
            throw new Exception("表MDA_PARAMETER_DATA_NAME不支持通过GUID进行删除!");
        }

        public void Delete(TableDataConfig entity)
        {
            string sql = string.Format("delete form MDA_PARAMETER_INDEX_NAME t where t.id={0}", entity.ID.ToString());
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void DeleteAll()
        {
            throw new Exception("您不能一次删除所有的索引字段,因为那是一个非常危险,同时也是不能接受为事情,为了对数据负责,请您逐一进行删除!");
        }

        public TableDataConfig GetEntityByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            if (dtSource.Rows.Count != 1)
            {
                throw new Exception("当前条件在TableDataConfig表中不能找到唯一一条记录,请重新设置条件再进行查询!");
            }
            return obc.ChangeTableToObject<TableDataConfig>(dtSource).FirstOrDefault();
        }

        public TableDataConfig GetEntityByGuid(Guid id)
        {
            throw new Exception("表MDA_PARAMETER_DATA_NAME不支持通过GUID查询单条数据,请通过其他方式查询!");
        }

        public List<TableDataConfig> GetEntitysByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            return obc.ChangeTableToObject<TableDataConfig>(dtSource).ToList();
        }

        public DataTable GetTable(string condition)
        {
            string sql = string.Format("Select * from MDA_PARAMETER_DATA_NAME t {0} order by t.ORDER_INDEX asc", condition);
            var con = dbContext.GetDbConnection();
            try
            {
                con.Open();
                return dbContext.GetTable(dbContext.GetDbCommand(con), sql);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public void Update(TableDataConfig entity)
        {
            throw new Exception("值字段不支持变更,因为涉及到主数据值得变化,如果确实很有必要进行变更,请与公司领导进行申请,再通过后台方式对所有数据进行变更");
        }
        private string GetInsertSql(TableDataConfig entity)
        {
            return string.Format("Insert into MDA_PARAMETER_DATA_NAME(MAINID,NAME,DATATYPE,NEEDBASEVALUE,NEEDLHVALUE,NEEDLLHHVALUE,ORDER_INDEX,CREATE_USER)values('{0}','{1}','{2}','{3}','{4}'，'{5}',{6},'SC')"
                , entity.MainID, entity.Name, entity.DataType, entity.NeedBaseValue,entity.NeedLHValue,entity.NeedLLHHValue, entity.Order_Index);
        }
    }
}
